function ImagePick(w){
	//GUI
	var c = $('<div class="image-picker" />');
	document.body.wyg = w;
	
	c.dialog({
		modal: false,
		width: 600,
		height: 400,
		title: 'Odaberite sliku',
		resizable: false,
		beforeClose: function(){
			c.remove();
		}
	});
	
	//Load images
	ImageLoad();
}
function ImageLoad(){
	var c = $('.image-picker');
	var w = document.body.wyg;
	c.empty();
	$.ajax({
		url: '/admin/userimages',
		beforeSend: function(){
			$('.throbber').show();
		},
		complete: function(req){
			$('.throbber').hide();
			if(req.status==200){
				var images = eval(req.responseText);
				var li;
				var ul = $('<ul />');
				c.append(ul);
				for(var i=0; i<images.length; i++){
					li = $('<li title="'+images[i]+'" />');
					li.html('<div class="image"><img src="/admin/userimage_show/thumb/'+images[i]+'" /></div>');
					li.append('<div class="label">'+images[i]+'</div>');
					ul.append(li);
				}
				
				c.append('<iframe src="" id="uploadframe" name="uploadframe" style="display:none" />');
				var f = $('<form target="uploadframe" onsubmit="ImageUpload();" action="/admin/userimage_upload" method="post" enctype="multipart/form-data" />');
				f.append('<input type="hidden" name="a" value="upload" />');
				f.append('<input type="file" name="userimage" />');
				f.append('<input type="submit" value="Pošalji" class="btn" />');
				
				var sel = $('<input type="button" class="right btn select" value="Odaberi sliku" />');
				sel.click(function(){ImageSelect(w, ul);});
				f.append(sel);
				
				var del = $('<input type="button" class="right btn" value="Obriši sliku" />');
				del.click(function(){ImageDelete(w, ul);});
				f.append(del);				
				
				c.append(f);
				
				ul.find("li").click(function(){
					$(this).parent().find('li').removeClass('selected');
					$(this).addClass('selected');
				});
				ul.find("li").dblclick(function(){
					$(this).parent().parent().find("input.select").click();
				});
			}
		}
	});
}
function ImageSelect(w, ul){
	var img = ul.find('li.selected').attr("title");
	if(img){
		$('.image-picker').remove();
		w.editorDoc.execCommand("insertImage", false, '/admin/userimage_show/normal/'+img);
	}
}
function ImageDelete(w, ul){
	var img = ul.find('li.selected').attr("title");
	if(img){
		if(confirm('Da li želite da obrišete sliku?')){
			$.ajax({
				url: '/admin/userimage_delete/'+img,
				beforeSend: function(){
					$(".throbber").show();
				},
				complete: function(req){
					$(".throbber").hide();
					if(req.status==200){
						var status = parseInt(req.getResponseHeader("X-Status"));
						if(!status){
							ul.find("li").each(function(){
								if($(this).attr("title")==img){
									$(this).remove();
								}
							});
						}
					}
				}
			});
		}
	}
}
function ImageUpload(){
	$('.throbber').show();
	$("#uploadframe").attr("onload", "ImageOnUpload()");
}
function ImageOnUpload(){
	var frame = $('#uploadframe');
	var fBody = $(frame[0].contentWindow.document.body);
	$('.throbber').hide();
	ImageLoad();
}